<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Course extends MY_Controller {

	function __construct(){
		parent::__construct();
		//课程推荐
		$this->load->model('Index/CourseModel', 'item');
	}	
	
	//列表
	public function get_list(){		
		$page		= $this->input->post('page');
		$size		= $this->input->post('size');
		
		if($page<1)$page=1;
		if($size<1)$size=10;
		
		$arr = array(
			
		);
		
		$res = $this->item->get_list($arr,$page,$size);
		
		if(($page-1)*$size>$res['count']){
			$this->err_out('没有了',10);
		}
	
		$data['result_code'] = 0;
		$data['message']     = '获取列表成功';
		$data['data'] = array('count'=>$res['count'],'list'=>$res['list']);
	
		$this->out($data);
	}
	
	//详细
	public function get_info(){		
		$user_id = $this->user_id;
		$id		 = $this->input->post('id',null,'0');
		$info = $this->item->get_info($id,$user_id);
		if(!$info){
			$this->err_out('数据不存在');
		}
		
		
		$data['result_code'] = 0;
		$data['message']     = '成功';
		$data['data'] 		 = $info;
	
		$this->out($data);
	}
	
	//全部
	public function get_lists(){
		$res = $this->item->get_lists();
		
		$data['result_code'] = 0;
		$data['message']     = '获取成功';
		$data['data'] 		 = $res;	
		$this->out($data);
	}
		
	//收藏课程
	public function add_collection(){
		$user_id = $this->user_id;
		$id		= $this->input->post('id',null,'0');
		
		$info = $this->item->get_info($id);
		if(!$info){
			$this->err_out('课程不存在');
		}
		
		$this->load->model('Index/CourseCollectionModel', 'collection');
		$row = $this->collection->get_info($user_id,$id);
		
		$res = '';
		if(!$row){
			$arr = array(
				"user_id" => $user_id,
				"cid"	  => $id,
				"addtime" => time()
			);
			$res = $this->collection->add($arr);		
		}
		
		$data['result_code'] = $res?0:1;
		$data['message']     = $res?'收藏成功':'收藏失败';
		$data['data'] 		 = '';	
		$this->out($data);
	}
	
	//删除收藏
	public function del_collection(){
		$user_id = $this->user_id;
		$id		= $this->input->post('id',null,'0');
		
		
		$info = $this->item->get_info($id);
		if(!$info){
			$this->err_out('课程不存在');
		}
		
		$this->load->model('Index/CourseCollectionModel', 'collection');
		$row = $this->collection->get_info($user_id,$id);
	
		$res = '';
		if($row){		
			$res = $this->collection->del(" user_id='{$user_id}' and cid='{$id}' ");		
		}
		
		$data['result_code'] = $res?0:1;
		$data['message']     = $res?'取消成功':'取消失败';
		$data['data'] 		 = '';	
		$this->out($data);
	}
	
	//取消预约
	public function del_appoint(){
		$user_id = $this->user_id;
		$id	= $this->input->post('id',null,'0');//预约id
		$time = time();
		
		$this->load->model('User/UserAppointModel', 'appoint');
		$info = $this->appoint->get_info($id);
		if(!$info){
			$this->err_out('预约不存在');
		}
		if($user_id!=$info['user_id']){
			$this->err_out('不能操作');
		}
		if($info['status']==1){
			$this->err_out('已签到了，不能取消预约');
		}
		if($info['status']==2){
			$this->err_out('已取消预约');
		}
		//开始前两小时不可取消预约
		if($info['sdate'] && $info['stime']){
			$start_time = $info['sdate'].' '.$info['stime'].':00';
			$start_time = strtotime($start_time);
			if($start_time - $time <= 2*60*60){
				//$this->err_out('时间超时，请联系教练员');
			}
		}		
		
		$res = $this->appoint->edit(array("status"=>2)," id= {$id}");
		
		//加回卡的使用次数
		$this->load->model('User/UserCardModel','card');
		$card_info = $this->card->get_info($info['card_id']);
		if($res){
			$num = $card_info['num'] + 1;
			$res_card = $this->card->edit(array("num"=>$num)," id='{$info['card_id']}' ");			
		}
		
		$data['result_code'] = $res?0:1;
		$data['message']     = $res?'取消成功':'取消失败';
		$data['data'] 		 = '';	
		$this->out($data);
	}
	
	//添加签到
	public function add_sign(){
		$user_id = $this->user_id;
		$id	= $this->input->post('id',null,'0');//预约id
		$time = time();
		
		$this->load->model('User/UserAppointModel', 'appoint');
		$info = $this->appoint->get_info($id);
		if(!$info){
			$this->err_out('预约不存在');
		}
		if($user_id!=$info['user_id']){
			$this->err_out('不能操作');
		}
		if($info['status']==1){
			$this->err_out('不能反复签到');
		}
		if($info['status']==2){
			$this->err_out('预约已取消');
		}
		
		$res = $this->appoint->edit(array("status"=>1)," id= {$id}");
		
		//添加动作记录
		if($res){			
			//动作id
			$course_info =  $this->item->unique_get_info("id",$info['cid']);
			$sport = $course_info['sport'];
			if($sport){			
				$this->load->model('User/UserSportModel', 'sport');
				$sport = explode(",",$sport);
				foreach($sport as $v){
					$arr = array(
						"user_id"	=> $user_id,
						"pid"		=> $id,
						"sid"		=> $v,
						"addtime"	=> $time,						
					);
					$this->sport->add($arr);
				}
			}
		}
		
		$data['result_code'] = $res?0:1;
		$data['message']     = $res?'签到成功':'签到失败';
		$data['data'] 		 = '';	
		$this->out($data);
	}	
	
}
